package com.welyo {
	import com.welyo.model.Model;
	import com.welyo.model.SubMenuItemVO;
	import com.welyo.view.Menu;
	import com.welyo.view.PanContent;
	import fl.motion.easing.Sine;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.navigateToURL;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextFieldAutoSize;
	import gs.easing.Expo;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author vu.doan
	 */
	public class Controller extends Sprite {
		
		private var menuContent			:Sprite;
		private var arrSubItem			:Array;
		private var arrPan				:Array;
		private var arrPanProp			:Array;
		private var arrScale			:Array;
		private var arrContent			:Array;
		private var currentContent		:int;
		
		private var loader				:Loader;
		
		private var posLarge			:Point;
		
		public function Controller() {
			stage.stageFocusRect = false;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			arrSubItem = [];
			var sub:MovieClip;
			for (var i:int = 1; i < 4; i++) {
				sub = this.getChildByName("mcSubItem" + i) as MovieClip;
				if (sub) {
					initSubMenu(sub);
				}
			}
			
			mcFlower.mouseEnabled = mcFlower.mouseChildren = false;
			mcVongDua.mouseEnabled = mcVongDua.mouseChildren = false;
			
			mcBgLarge.mcPreloader.visible = false;
			mcBgLarge.mcPreloader.mouseChildren = mcBgLarge.mcPreloader.mouseEnabled = false;
			
			arrPan = [mcPan1, mcPan2, mcPan3];
			arrPanProp = [];
			arrScale = [];
			var rect:Rectangle;
			for (i = 0; i < arrPan.length; i++) {
				arrScale.push({scalex:arrPan[i].scaleX, scaley:arrPan[i].scaleY});
				rect = new Rectangle(arrPan[i].x, arrPan[i].y, arrPan[i].width, arrPan[i].height);
				(arrPan[i] as PanContent).Props = rect;
				(arrPan[i] as PanContent).PropScale = {scalex:arrPan[i].scaleX, scaley:arrPan[i].scaleY};
				arrPanProp.push(rect);
			}
			arrContent = [];
			currentContent = -1;
			
			mcBgLarge.alpha = 0;
			mcBgLarge.visible = false;
			posLarge = new Point(mcBgLarge.x, mcBgLarge.y);
			mcBgLarge.scaleX = mcBgLarge.scaleY = 0.4;
			mcBgLarge.x = posLarge.x + 300;
			mcBgLarge.y = posLarge.y + 300;
			
			var path:String = (root.loaderInfo.parameters.xmlpath) ? root.loaderInfo.parameters.xmlpath : "../xml/config.xml";
			path = path.replace(/;/gi, "&");
			
			loadConfig(path);
		}
		
		private function initSubMenu(sub:MovieClip):void {
			sub.mcText.alpha = 0;
			sub.mcText.txtText.autoSize = TextFieldAutoSize.LEFT;
			sub.mcText.txtTextShadow.autoSize = TextFieldAutoSize.LEFT;
			arrSubItem.push(sub);
			sub.mcKey.mouseEnabled = sub.mcBg.mouseEnabled = false;
			sub.mcHit.buttonMode = true;
			sub.mcHit.addEventListener(MouseEvent.CLICK, onClickSubMenuHandler);
			sub.mcHit.addEventListener(MouseEvent.MOUSE_OVER, onOverSubMenuHandler);
			sub.mcHit.addEventListener(MouseEvent.MOUSE_OUT, onOutSubMenuHandler);
		}
		
		private function onOverSubMenuHandler(evt:MouseEvent):void {
			var index:int = arrSubItem.indexOf(evt.currentTarget.parent);
			
			if (index == 0) {
				(arrPan[0] as PanContent).Props = arrPanProp[2];
				(arrPan[1] as PanContent).Props = arrPanProp[0];
				(arrPan[2] as PanContent).Props = arrPanProp[1];
				
				TweenLite.to(arrPan[1], 0.5, {x:arrPanProp[0].x, y:arrPanProp[0].y, width:arrPanProp[0].width, height:arrPanProp[0].height, scaleX:arrScale[0].scalex, scaleY:arrScale[0].scaley});
				TweenLite.to(arrPan[2], 0.5, {x:arrPanProp[1].x, y:arrPanProp[1].y, width:arrPanProp[1].width, height:arrPanProp[1].height, scaleX:arrScale[1].scalex, scaleY:arrScale[1].scaley});
			} else if (index == 1) {
				(arrPan[1] as PanContent).Props = arrPanProp[2];
				(arrPan[0] as PanContent).Props = arrPanProp[0];
				(arrPan[2] as PanContent).Props = arrPanProp[1];
				
				TweenLite.to(arrPan[0], 0.5, {x:arrPanProp[0].x, y:arrPanProp[0].y, width:arrPanProp[0].width, height:arrPanProp[0].height, scaleX:arrScale[0].scalex, scaleY:arrScale[0].scaley});
				TweenLite.to(arrPan[2], 0.5, {x:arrPanProp[1].x, y:arrPanProp[1].y, width:arrPanProp[1].width, height:arrPanProp[1].height, scaleX:arrScale[1].scalex, scaleY:arrScale[1].scaley});
			} else if (index == 2) {
				(arrPan[2] as PanContent).Props = arrPanProp[2];
				(arrPan[0] as PanContent).Props = arrPanProp[0];
				(arrPan[1] as PanContent).Props = arrPanProp[1];
				
				TweenLite.to(arrPan[0], 0.5, {x:arrPanProp[0].x, y:arrPanProp[0].y, width:arrPanProp[0].width, height:arrPanProp[0].height, scaleX:arrScale[0].scalex, scaleY:arrScale[0].scaley});
				TweenLite.to(arrPan[1], 0.5, {x:arrPanProp[1].x, y:arrPanProp[1].y, width:arrPanProp[1].width, height:arrPanProp[1].height, scaleX:arrScale[1].scalex, scaleY:arrScale[1].scaley});
			}
			
			for (var i:int = 0; i < arrPan.length; i++) {
				mcPanHolder.addChild(arrPan[i]);
			}
			mcPanHolder.addChild(arrPan[index]);
			TweenLite.to(arrPan[index], 0.5, {x:arrPanProp[2].x, y:arrPanProp[2].y, width:arrPanProp[2].width, height:arrPanProp[2].height, scaleX:arrScale[2].scalex, scaleY:arrScale[2].scaley});
		}
		
		private function onOutSubMenuHandler(evt:MouseEvent):void {
			
		}
		
		private function onClickSubMenuHandler(evt:MouseEvent):void {
			var index:int = arrSubItem.indexOf(evt.currentTarget.parent);
			
			if (arrSubItem[currentContent]) {
				(arrSubItem[currentContent] as MovieClip).mouseChildren = (arrSubItem[currentContent] as MovieClip).mouseEnabled = true;
				arrSubItem[currentContent].mcBg.gotoAndStop(1);
			}
			
			if (currentContent != 1 && arrContent[currentContent]) {
				arrContent[currentContent].dispose();
			}
			
			currentContent = index;
			arrSubItem[index].mcBg.gotoAndStop(2);
			(arrSubItem[currentContent] as MovieClip).mouseChildren = (arrSubItem[currentContent] as MovieClip).mouseEnabled = false;
			showPan();
			
			try {
				ExternalInterface.call("turnOffSound");
			} catch(e:Error) {}
		}
		
		private function onOpenPanHandler(evt:Event):void {
			var index:int = arrPan.indexOf(evt.currentTarget);
			
			if (arrSubItem[currentContent]) {
				(arrSubItem[currentContent] as MovieClip).mouseChildren = (arrSubItem[currentContent] as MovieClip).mouseEnabled = true;
				arrSubItem[currentContent].mcBg.gotoAndStop(1);
			}
			
			if (currentContent != 1 && arrContent[currentContent]) {
				arrContent[currentContent].dispose();
			}
			
			currentContent = index;
			arrSubItem[index].mcBg.gotoAndStop(2);
			(arrSubItem[currentContent] as MovieClip).mouseChildren = (arrSubItem[currentContent] as MovieClip).mouseEnabled = false;
			showPan();
			
			try {
				ExternalInterface.call("turnOffSound");
			} catch(e:Error) {}
		}
		
		private function showPan():void {
			this.mouseChildren = this.mouseEnabled  = false;
			
			for (var i:int = 0; i < arrPan.length; i++) {
				(arrPan[i] as PanContent).hide(0);
			}
			
			if (arrContent[currentContent]) {
				showContent(arrContent[currentContent]);
			} else {
				var arr:Array = Model.getIntance().SubMenus;
				loadContent((arr[currentContent] as SubMenuItemVO).Path);
			}
		}
		
		private function loadContent(path:String):void {
			mcBgLarge.mcPreloader.visible = true;
			mcBgLarge.visible = true;
			TweenLite.delayedCall(0.1, onCompleteDelayLargeHandler);
			
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadContentHandler);
			loader.load(new URLRequest(path));
		}
		
		private function onCompleteLoadContentHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadContentHandler);
			
			var content:* = evt.currentTarget.content;
			var arr:Array = Model.getIntance().SubMenus;
			mcBgLarge.mcPreloader.visible = false;
			
			if (content.init) {
				content.init((arr[currentContent] as SubMenuItemVO).URLData);
			}
			arrContent[currentContent] = content;
			
			showContent(content);
		}
		
		private function showContent(content:*):void {
			this.mouseChildren = this.mouseEnabled  = true;
			
			while (mcBgLarge.mcHolder.numChildren > 0) {
				mcBgLarge.mcHolder.removeChildAt(0);
			}
			
			if (content) {
				content.show();
				content.alpha = 0;
				TweenLite.to(content, 0.5, { alpha:1, ease:Sine.easeOut } );
				
				mcBgLarge.mcHolder.addChild(content);
				if (currentContent == 1) {
					content.x = content.y = 4;
				}
			}
		}
		
		private function onCompleteDelayLargeHandler():void {
			TweenLite.to(mcBgLarge, 0.5, {alpha:1, ease:Sine.easeOut, scaleX:1, scaleY:1, x:posLarge.x, y:posLarge.y});
		}
		
		private function loadConfig(path:String):void {
			var ldr:URLLoader = new URLLoader();
			ldr.addEventListener(Event.COMPLETE, onCompleteLoadConfigHandler);
			ldr.load(new URLRequest(path));
		}
		
		private function onCompleteLoadConfigHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadConfigHandler);
			
			var data:XML = new XML(evt.currentTarget.data);
			Model.getIntance().parseData(data);
			var arr:Array = Model.getIntance().SubMenus;
			for (var i:int = 0; i < arrSubItem.length; i++) {
				(arrSubItem[i] as MovieClip).mcText.txtText.text = (arr[i] as SubMenuItemVO).Label;
				(arrSubItem[i] as MovieClip).mcText.txtTextShadow.text = (arr[i] as SubMenuItemVO).Label;
			}
			
			for (i = 0; i < arrPan.length; i++) {
				mcPanHolder.addChild(arrPan[i]);
				(arrPan[i] as PanContent).init((arr[i] as SubMenuItemVO).Thumbnail);
				(arrPan[i] as PanContent).addEventListener(PanContent.OPEN_PAN, onOpenPanHandler);
			}
			
			var pathMenu:String = data.menupath;
			
			mcLogo.buttonMode = true;
			mcLogo.addEventListener(MouseEvent.CLICK, onClickLogoHandler);
			
			loadMenu(pathMenu);
		}
		
		private function onClickLogoHandler(evt:MouseEvent):void {
			var path:String = Model.getIntance().LinkLogo;
			navigateToURL(new URLRequest(path), "_self");
		}
		
		private function loadMenu(path:String):void {
			var ldr:Loader = new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onCOmpleteLoadMenuHandler);
			ldr.load(new URLRequest(path));
		}
		
		private function onCOmpleteLoadMenuHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCOmpleteLoadMenuHandler);
			
			menuContent = evt.currentTarget.content;
			
			(menuContent as Menu).setURL(Model.getIntance().Menus);
			
			mcMenuHolder.addChild(menuContent);
			menuContent.y = mcVector.y + mcVector.height - 4 - 32;
			(menuContent as Menu).activeMenu(2);
			
			// show submenu
			showSubMenu();
		}
		
		private function showSubMenu():void {
			for (var i:int = 0; i < arrSubItem.length; i++) {
				TweenLite.to(arrSubItem[i], 0.5, {delay: 0.2 * i, onComplete: onCompleteDelayHandler,
							onCompleteParams:[i]});
			}
		}
		
		private function onCompleteDelayHandler(index:int):void {
			if (arrSubItem[index]) {
				arrSubItem[index].mcText.x = arrSubItem[index].mcBg.x + (arrSubItem[index].mcBg.width - arrSubItem[index].mcText.width) / 2;
				(arrSubItem[index] as MovieClip).gotoAndPlay(2);
				TweenLite.to(arrSubItem[index].mcText, 0.3, {alpha:1, delay:0.5, ease:Expo.easeOut});
			}
		}
	}
}